package com.bbc.service.shop.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbc.base.BaseMessage;
import com.bbc.base.page.MysqlPaginatedArrayList;
import com.bbc.base.page.PaginatedList;
import com.bbc.entity.shop.Shop;
import com.bbc.entity.shop.ShopQuery;
import com.bbc.mapper.shop.ShopMapper;
import com.bbc.service.shop.ShopService;
import com.bbc.third.taobao.TBApi;
import com.opensymphony.oscache.util.StringUtil;
import com.taobao.api.response.ShopGetResponse;

@Service
public class ShopServiceImpl implements ShopService {
	
	@Autowired
	private transient TBApi tBApi;
	
	@Autowired
	private transient ShopMapper shopMapper;

	@Override
	public Integer addShop(Shop shop) {
		return shopMapper.addShop(shop);
	}

	@Override
	public void updateShop(Shop shop) {
		shopMapper.updateShop(shop);
	}

	@Override
	public List<Shop> findShop(ShopQuery query, int page, int pageSize) {
		PaginatedList<Shop> list = new MysqlPaginatedArrayList<Shop>(page,pageSize);
		list.setTotalItem(shopMapper.findShopCount(query));
		query.setStartRow(list.getStartRow());
		query.setEndRow(list.getEndRow());
		list.addAll(shopMapper.findShop(query));
		return list;
	}

	@Override
	public Shop findShopById(Integer id) {
		if (id == null) {
			return null;
		}
		ShopQuery q = new ShopQuery();
		q.setId(id);
		int count = shopMapper.findShopCount(q);
		if (count == 1) {
			return shopMapper.findShop(q).get(0);
		} else {
			return null;
		}
	}

	@Override
	public Shop findShopByNick(String nick, String fromKey) {
		if (StringUtil.isEmpty(nick) || StringUtil.isEmpty(fromKey)) {
			return null;
		}
		ShopQuery q = new ShopQuery();
		q.setNick(nick);
		q.setFromKey(fromKey);
		int count = shopMapper.findShopCount(q);
		if (count == 1) {
			return shopMapper.findShop(q).get(0);
		} else {
			return null;
		}
	}

	@Override
	public BaseMessage addShopFromTBApi(String nick) {
		BaseMessage msg = new BaseMessage();
		if (StringUtil.isEmpty(nick)) {
			msg.setCode(101);
			msg.setInfo("nick为空");
		} else {
			ShopQuery q = new ShopQuery();
			q.setNick(nick);
			q.setFromKey(tBApi.key);
			int count = shopMapper.findShopCount(q);
			if (count == 1) {
				msg.setCode(109);
				msg.setInfo("数据已存在");
			} else {
				ShopGetResponse response = tBApi.getShop(nick);
				Shop shop = new Shop();
				shop.setCid(response.getShop().getCid().toString());
				shop.setDeliveryScore(response.getShop().getShopScore().getDeliveryScore());
				shop.setFromKey(tBApi.key);
				shop.setItemScore(response.getShop().getShopScore().getItemScore());
				shop.setNick(nick);
				shop.setPicPath(response.getShop().getPicPath());
				shop.setServiceScore(response.getShop().getShopScore().getServiceScore());
				shop.setSid(response.getShop().getSid());
				shop.setTitle(response.getShop().getTitle());
				shopMapper.addShop(shop);
				msg.setCode(100);
				msg.setInfo("操作成功");
				msg.setSuccess(true);
			}
		}
		return msg;
	}

}
